package top.as.sean.biz.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.as.sean.biz.system.IUserRole;
import top.as.sean.dao.dto.system.UserRoleCancel;
import top.as.sean.dao.entity.pojo.system.Menu;
import top.as.sean.dao.entity.pojo.system.UserRole;
import top.as.sean.dao.mapper.system.UserRoleMapper;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * 用户-角色
 *
 * @author: ArchieSean
 * @create: 2022/11/29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserRoleImpl implements IUserRole {
    /**
     * 用户-角色中间表
     */
    private final UserRoleMapper userRoleMapper;

    /**
     * 新增用户-角色
     *
     * @param roleIds 角色id
     * @param userId  用户id
     */
    @Override
    public void saveBatchIds(@NotNull List<Long> roleIds, @NotNull Long userId) {
        //删除
        userRoleMapper.delBatchByUserId(userId);
        List<UserRole> list = Lists.newArrayList();
        roleIds.forEach(roleId -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            list.add(userRole);
        });
        userRoleMapper.saveBatch(list);
    }

    /**
     * 删除用户
     *
     * @param ids 用户ids
     */
    @Override
    public void deleteByUserId(List<Long> ids) {
        userRoleMapper.deleteByUserId(ids);
    }

    /**
     * 根据角色id查询
     *
     * @param roleId 角色id
     * @return list
     */
    @Override
    public List<Long> queryByRoleId(Long roleId) {
        return userRoleMapper.queryByRoleId(roleId);
    }

    /**
     * 取消授权
     *
     * @param dto 入参
     */
    @Override
    public void cancel(UserRoleCancel dto) {
        userRoleMapper.cancel(dto);
    }

    /**
     * 批量新增
     *
     * @param dto 入参
     */
    @Override
    public void saveBatch(UserRoleCancel dto) {
        List<Long> userIds = dto.getUserIds();
        Long roleId = dto.getRoleId();
        List<UserRole> list = Lists.newArrayList();
        userIds.forEach(userId -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            list.add(userRole);
        });
        userRoleMapper.saveBatch(list);
    }

    /**
     * 查询角色名称
     *
     * @param userId 用户id
     * @return list
     */
    @Override
    public Set<String> queryRoleName(Object userId) {
        return userRoleMapper.queryRoleName(userId);
    }

    /**
     * 查询按钮权限标识符
     *
     * @param userId 用户id
     * @return list
     */
    @Override
    public Set<String> queryBtnList(Object userId,String menuTypeCode) {
        return userRoleMapper.queryBtnList(userId,menuTypeCode);
    }

    /**
     * 获取动态路由
     *
     * @param userId 用户id
     * @return list
     */
    @Override
    public List<Menu> queryMenuByUserId(Object userId) {
        return null;
    }
}
